package com.ks.BusAlarm;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

public class PreferenceUI extends PreferenceActivity {
	Preferences pref;

	SharedPreferences sp;
	SharedPreferences.Editor spe;
	Boolean PrefVibration;
	Boolean PrefSound;
	int PrefTimer;

	CheckBoxPreference checkPrefVibration;
	CheckBoxPreference checkPrefSound;
	EditTextPreference editPrefTimer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		sp = getSharedPreferences(getString(R.string.PREF_NAME), 0);
		spe = sp.edit();

		addPreferencesFromResource(R.layout.preferenceui);

		checkPrefVibration = (CheckBoxPreference) findPreference("prefVibration");

		PrefVibration = sp
				.getBoolean(getString(R.string.PREF_VIBRATION), false);
		checkPrefVibration.setChecked(PrefVibration);

		checkPrefVibration
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {

						spe.putBoolean(getString(R.string.PREF_VIBRATION),
								!PrefVibration);
						spe.commit();

						return false;
					}
				});

		checkPrefSound = (CheckBoxPreference) findPreference("prefSound");
		PrefSound = sp.getBoolean(getString(R.string.PREF_SOUND), false);
		checkPrefSound.setChecked(PrefSound);

		checkPrefSound
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {

						spe.putBoolean(getString(R.string.PREF_SOUND),
								!PrefSound);
						spe.commit();

						return false;
					}
				});

		editPrefTimer = (EditTextPreference) findPreference("editPrefTimer");
		PrefTimer = sp.getInt(getString(R.string.PREF_TIMER), 20);
		editPrefTimer.setDefaultValue((int)PrefTimer);
		
		
		
		editPrefTimer.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				spe.putInt(getString(R.string.PREF_TIMER), PrefTimer);
				spe.commit();
				ksLog.d("PrefTimer",PrefTimer);
				
				return true;
			}
		});
	}
}
